package com.zhaopin.demo;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class TestMap {


	public static void main(String[] args) {
		testMap(10_000*1_024);
	}
/**
	 * 测试Map
	 * @param  keyValueNum：键值对的数量 
	 */
	public static void testMap(int keyValueNum) {
		/**
		 * 创建map
		 */
		Map<Integer, Integer> map = new HashMap<Integer, Integer>();
		for(int i = 0; i < keyValueNum; i++){
			map.put(i, i);
		}

		/**
		 * entrySet遍历
		 */
		long start = System.currentTimeMillis();
		Iterator<Map.Entry<Integer, Integer>> iterator2 = map.entrySet().iterator();
		Map.Entry<Integer, Integer> entry = null;
		while(iterator2.hasNext()){
			entry = iterator2.next();
			int key = entry.getKey();
			int value = entry.getValue();
		}
		System.out.println("entrySet consume time = " + (System.currentTimeMillis() - start));
		/**
		 * keySet遍历
		 */
		start = System.currentTimeMillis();
		Iterator<Integer> iterator = map.keySet().iterator();
		while(iterator.hasNext()){
			int key = iterator.next();
			////效率低下原因在此，因为此处会再次遍历Map ，取得key对应的value值。 牛逼豁然开朗了
			int value = map.get(key);
		}
		System.out.println("keySet consume time = " + (System.currentTimeMillis() - start));
		start = System.currentTimeMillis();
		map.forEach((k,v)->{
			int key = k;
			int value= v;
		});
		System.out.println("forEach consume time = " + (System.currentTimeMillis() - start));		
	}
}

